from django.db import models
from django.utils import timezone
from company.models import UserProfile


# PPT模板表
class PresentationTemplate(models.Model):
    TEMPLATE_TYPE_CHOICES = [
        ('business_plan', '商业计划书'),
        ('pitch_deck', '路演PPT'),
        ('annual_report', '年度报告'),
        ('project_summary', '项目总结'),
    ]

    name = models.CharField(max_length=200, verbose_name='模板名称', db_comment='模板名称')
    template_type = models.CharField(max_length=50, choices=TEMPLATE_TYPE_CHOICES, verbose_name='模板类型', db_comment='模板类型:business_plan商业计划书,pitch_deck路演PPT,annual_report年度报告,project_summary项目总结')
    description = models.TextField(verbose_name='模板说明', db_comment='模板说明')
    template_config = models.JSONField(default=dict, verbose_name='模板配置', db_comment='模板配置JSON')
    preview_image = models.ImageField(upload_to='template_previews/', blank=True, null=True, verbose_name='预览图', db_comment='预览图路径')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_presentation_templates'
        verbose_name = 'PPT模板'
        verbose_name_plural = 'PPT模板'
        db_table_comment = 'PPT模板表'

    def __str__(self):
        return self.name


# 项目汇报表
class ProjectReport(models.Model):
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('generated', '已生成'),
        ('finalized', '已定稿'),
    ]

    user_profile = models.ForeignKey(
        UserProfile,
        on_delete=models.CASCADE,
        related_name='project_reports',
        verbose_name='创建者',
        db_comment='创建者ID'
    )
    template = models.ForeignKey(
        PresentationTemplate,
        on_delete=models.SET_NULL,
        null=True,
        related_name='project_reports',
        verbose_name='使用模板',
        db_comment='使用模板ID'
    )
    title = models.CharField(max_length=200, verbose_name='报告标题', db_comment='报告标题')
    content_data = models.JSONField(default=dict, verbose_name='内容数据', db_comment='内容数据JSON')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft', verbose_name='状态', db_comment='状态:draft草稿,generated已生成,finalized已定稿')
    file_path = models.FileField(upload_to='project_reports/', blank=True, null=True, verbose_name='文件路径', db_comment='文件路径')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        db_table = 'mentorx_project_reports'
        verbose_name = '项目汇报'
        verbose_name_plural = '项目汇报'
        ordering = ['-updated_at']
        db_table_comment = '项目汇报表'

    def __str__(self):
        return self.title


# 汇报历史版本表
class ReportVersion(models.Model):
    report = models.ForeignKey(
        ProjectReport,
        on_delete=models.CASCADE,
        related_name='versions',
        verbose_name='所属报告',
        db_comment='所属报告ID'
    )
    version_number = models.IntegerField(verbose_name='版本号', db_comment='版本号')
    content_snapshot = models.JSONField(verbose_name='内容快照', db_comment='内容快照JSON')
    file_path = models.FileField(upload_to='report_versions/', blank=True, null=True, verbose_name='文件路径', db_comment='文件路径')
    created_at = models.DateTimeField(default=timezone.now, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'mentorx_report_versions'
        verbose_name = '汇报版本'
        verbose_name_plural = '汇报版本'
        ordering = ['-version_number']
        unique_together = ['report', 'version_number']
        db_table_comment = '汇报版本表'

    def __str__(self):
        return f"{self.report.title} - v{self.version_number}"
